//
//  CPYSongListTypeViewController.m
//  TTMusic
//
//  Created by chenpengyu on 16/4/5.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "CPYSongListTypeViewController.h"
#import "CPYSongListTypeModel.h"
#import "CPYSongListTypeChildrenModel.h"
#import "CPYSongListTypeChildrenCollectionViewCell.h"
#import "CPYSongListTypeTableViewCell.h"
#import "TTMusicSongListViewController.h"
@interface CPYSongListTypeViewController ()<UITableViewDelegate, UITableViewDataSource, TTMusicBaseTableViewCellDelegate>

@property (nonatomic, strong) TTMusicBaseCollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *dataChildrenSource;
@property (nonatomic, strong) TTMusicBaseTableView *tableView;
@end

@implementation CPYSongListTypeViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    
   }
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   

    [self creatNavButtonItem];
    [self creatTableView];
    [self createData];
}
- (void)creatNavButtonItem {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]  initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
}
- (void)leftBarButtonAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)creatTableView {
    self.tableView = [[TTMusicBaseTableView alloc] initWithFrame:CGRectMake(0, 0, SysWidth, SysHeight - 64)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[CPYSongListTypeTableViewCell class] forCellReuseIdentifier:@"CPYSongListTypeTableViewCellIdentifier"];
    [self.view addSubview:_tableView];
}

- (void)createData{

     self.dataSource = [NSMutableArray array];
    [TTMusicAFNetworkTool GETNetWithUrl:@"http://api.dongting.com/song/tag/songlist" body:nil headerFile:nil response:TTMusicJSON success:^(id result) {
            for (NSDictionary *dic in result[@"data"]) {
                    CPYSongListTypeModel *songListTypeModel = [[CPYSongListTypeModel alloc] init];
                    [songListTypeModel setValuesForKeysWithDictionary:dic];
                    [_dataSource addObject:songListTypeModel];
                }
                [_tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_dataSource.count) {
         return _dataSource.count + 1;
    }
    return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return nil;
    }
    if (_dataSource) {
            CPYSongListTypeModel *songListTypeModel = _dataSource[section - 1];
            return songListTypeModel.tag;
    }
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        TTMusicBaseLabel *label = [[TTMusicBaseLabel alloc] initWithFrame:CGRectMake(5, 0, SysWidth - 10, 40)];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"全部";
        label.layer.borderWidth = 1.0f;
        label.font = [UIFont systemFontOfSize:23];
        [cell addSubview:label];
        return cell;
    }else {
        CPYSongListTypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CPYSongListTypeTableViewCellIdentifier"];
        CPYSongListTypeModel *songListTypeModel = _dataSource[indexPath.section - 1];
        self.dataChildrenSource = [NSMutableArray array];
        for (NSDictionary *dic in songListTypeModel.children) {
            CPYSongListTypeChildrenModel *typeChildrenModel = [[CPYSongListTypeChildrenModel alloc] init];
            [typeChildrenModel setValuesForKeysWithDictionary:dic];
            [_dataChildrenSource addObject:typeChildrenModel];
        }
        cell.delegate = self;
        cell.dataTypeChildrenSource = _dataChildrenSource;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
            return 40;
            break;
        case 1:
            return 335;
            break;
        case 2:
            return 225;
            break;
        case 3:
            return 335;
            break;
        case 4:
            return 170;
            break;
        case 5:
            return 115;
            break;
        default:
            break;
    }
    return 0;
}
- (void)sendTag:(NSString *)tag {
    
    self.blockOfButtonType(tag);
    
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
